package utils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.faces.bean.ApplicationScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter("conversorEntidad")
@ApplicationScoped
public class Conversor implements Converter {

    private static Map<String, Object> converterMap = new ConcurrentHashMap<String, Object>();

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String key) {
        return converterMap.get(key);
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object entidad) {
        if (entidad == null) {
            return null;
        }
        String key = entidad.toString();
        converterMap.put(key, entidad);
        return key;
    }

}
